package com.weilan.blog.picture.controller;

import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.global.SysConf;
import com.weilan.blog.picture.service.PictureService;
import com.weilan.blog.picture.vo.PictureVO;
import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/10 15:35
 */
@Slf4j
@RestController
@RequestMapping("/picture")
public class PictureController {
    @Autowired
    private PictureService pictureService;

    /**
     * 获取图片列表
     * @param pictureVO
     * @param result
     * @return
     */
    @PostMapping(value = "/getList")
    public String getList(@Validated({GetList.class}) @RequestBody PictureVO pictureVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.result(SysConf.SUCCESS, pictureService.getPageList(pictureVO));
    }

    /**
     * 增加图片
     * @param pictureVOList
     * @param result
     * @return
     */
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody List<PictureVO> pictureVOList, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return pictureService.addPicture(pictureVOList);
    }

    /**
     * 编辑图片
     * @param pictureVO
     * @param result
     * @return
     */
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody PictureVO pictureVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return pictureService.editPicture(pictureVO);
    }

    /**
     * 批量删除图片
     * @param uids
     * @return String
     */
    @DeleteMapping("/{uids}")
    public String delete(@PathVariable("uids") Set<String> uids) {
        return pictureService.deleteBatchPicture(uids);
    }

    /**
     * 通过图片Uid将图片设为封面
     * @param pictureVO
     * @param result
     * @return
     */
    @PostMapping("/setCover")
    public String setCover(@Validated({Update.class}) @RequestBody PictureVO pictureVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return pictureService.setPictureCover(pictureVO);
    }
}
